﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Wicresoft.Framework.Authentication;
using Wicresoft.Common;

public partial class Page_Duty_Week2 : BasePage
{
    #region MeetingRoomGroups
    private DataTable MeetingRoomGroups
    {
        get
        {
            if (ViewState["MeetingRoomGroups"] != null)
                return (DataTable)ViewState["MeetingRoomGroups"];
            else
                return null;
        }
        set
        {
            ViewState["MeetingRoomGroups"] = value;
        }
    }
    #endregion

    #region bEdit
    private bool bEdit
    {
        get
        {
            if (ViewState["bEdit"] != null)
                return (bool)ViewState["bEdit"];
            else
                return false;
        }
        set
        {
            ViewState["bEdit"] = value;
        }
    }
    #endregion

    #region bSpecial
    private bool bSpecial
    {
        get
        {
            if (ViewState["bSpecial"] != null)
                return (bool)ViewState["bSpecial"];
            else
                return false;
        }
        set
        {
            ViewState["bSpecial"] = value;
        }
    }
    #endregion

    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (!Page.IsPostBack)
        {


            for (int i = DateTime.Today.Year - 3; i <= DateTime.Today.Year + 5; i++)
            {
                this.ddlYear.Items.Add(i.ToString());
            }
            this.ddlYear.SelectedIndex = 3;

            for (int i = 1; i <= 12; i++)
            {
                this.ddlMonth.Items.Add(i.ToString());
            }
            this.ddlMonth.SelectedIndex = DateTime.Today.Month - 1;

            LoadData();
        }


    }



    private void LoadData()
    {
        //获取当天所在周的第一天
        DateTime dtTemp;
        if (Request.Form["txtDate"] == null || Request.Form["txtDate"].ToString() == "")
        {
            dtTemp = DateTime.Today;
            while (!dtTemp.DayOfWeek.ToString().Equals("Monday"))
                dtTemp = dtTemp.AddDays(-1);
        }
        else
            dtTemp = DateTime.Parse(Request.Form["txtDate"].ToString());

        initTable(dtTemp);
    }

    /// <summary>
    /// 获取会议室周使用信息
    /// </summary>
    /// <param name="dtTemp">周起始日</param>
    private void initTable(DateTime dtTemp)
    {
        String StartTime = dtTemp.ToString("yyyy-MM-dd");
        String EndTime = dtTemp.AddDays(7).ToString("yyyy-MM-dd");
        //String EndTime = dtTemp.AddDays(5).ToString("yyyy-MM-dd");


        DataTable dtRoom = new DataTable();
        dtRoom.Columns.Add("Name");
        dtRoom.Columns.Add("Time");
        string[] names = new string[] { "白天", "上午", "下午", "晚上" };
        string[] times = new string[] { "08:00-16:30", "08:00-12:00", "12:00-16:30", "16:30-32:00" };
        for (int i = 0; i < names.Length; i++)
        {
            DataRow newdr = dtRoom.NewRow();
            newdr["Name"] = names[i];
            newdr["Time"] = times[i];
            dtRoom.Rows.Add(newdr);

        }




        DataTable dtApply = DataAccess.ExecuteSql("Attendences.ConnectionString",
            @"select UserDisplayName,AttendTimeStart,AttendTimeEnd from View_Attend_All where AttendTimeStart >'" + StartTime + "' and AttendTimeStart <='" + EndTime + "'  order by ordernum").Tables[0];
        DrawMainTable(dtRoom, dtApply, dtTemp);
        this.txtDate.Text = dtTemp.ToString("yyyy-MM-dd");
    }


    private void DrawMainTable(DataTable dtRoom, DataTable dtApply, DateTime dtTemp)
    {
        this.tbRoomApply.Controls.Clear();
        DateTime StartTime = dtTemp;
        TableRow HeaderTR = new TableRow();
        for (int i = 0; i < 8; i++)
       
        {
            TableCell HeaderTC = new TableCell();
            if (i == 0)
            {
                HeaderTC.ColumnSpan = 1;
                HeaderTC.RowSpan = 2;
                HeaderTC.Width = System.Web.UI.WebControls.Unit.Percentage(8);
                int iDay = dtTemp.Day;
                int iWeek, curMonth, curYear;
                if (dtTemp.Month != dtTemp.AddDays(6).Month)
                {
                    iWeek = iDay - 7 * (int)(iDay / 7);
                    if (iWeek > 1)
                        iWeek = (int)(iDay / 7) + 2;
                    else
                        iWeek = (int)(iDay / 7) + 1;
                    this.lblWeekNum.Text = dtTemp.Year + "年" + dtTemp.Month + "月 第" + iWeek + "周";

                    if (dtTemp.Month != 12)
                    {
                        curMonth = dtTemp.Month + 1;
                        this.lblWeekNum.Text += "(" + dtTemp.Year + "年" + curMonth + "月 第1周)";
                    }
                    else
                    {
                        curYear = dtTemp.Year + 1;
                        this.lblWeekNum.Text += "(" + curYear + "年1月 第1周)";
                    }
                }
                else
                {
                    iWeek = iDay - 7 * (int)(iDay / 7);
                    if (iWeek > 1)
                        iWeek = (int)(iDay / 7) + 2;
                    else
                        iWeek = (int)(iDay / 7) + 1;
                    this.lblWeekNum.Text = dtTemp.Year + "年" + dtTemp.Month + "月 第" + iWeek + "周";
                }
            }
            else
            {
                //HeaderTC.ColumnSpan = WebCommon.CoverCount;
                HeaderTC.Text = ConvertNumToWeek(i);
                HeaderTC.CssClass = "TableHeader";
            }
            HeaderTC.Width = Unit.Percentage(12.5);
            HeaderTR.Cells.Add(HeaderTC);
        }
        this.tbRoomApply.Rows.Add(HeaderTR);

        HeaderTR = new TableRow();
        for (int i = 0; i < 7; i++)
        //for (int i = 0; i < 5; i++)
        {
            TableCell HeaderTC = new TableCell();
            DateTime dtCurDate = dtTemp.AddDays(i).Date;
            //HeaderTC.ColumnSpan = WebCommon.CoverCount;
            HeaderTC.Text =   dtCurDate.Month + "月" + dtCurDate.Day + "日";
            HeaderTC.BorderWidth = 1;
            HeaderTC.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
            HeaderTR.Cells.Add(HeaderTC);
        }
        this.tbRoomApply.Rows.Add(HeaderTR);




        for (int i = 0; i < dtRoom.Rows.Count; i++)
        {
            TableRow TR = new TableRow();
            TR.Width = System.Web.UI.WebControls.Unit.Percentage(100);
            TableCell TC = new TableCell();
            TC.Width = System.Web.UI.WebControls.Unit.Percentage(7);
            TC.RowSpan = 1;
            TC.Height = 60;
            TC.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;

            //RoomInfo += "描述："+ dtRoom.Rows[i][vwMeetingRooms.SQLCOL_DESCRIPTION].ToString() + Environment.NewLine;
            TC.Text = dtRoom.Rows[i]["Name"].ToString();
            TC.ToolTip = dtRoom.Rows[i]["Name"].ToString();
            TC.Style.Add("cursor", "hand");
            TC.BorderWidth = 1;
            TC.CssClass = "Room";
            TR.Cells.Add(TC);

            for (int j = 0; j < 7; j++)
            {
                TC = new TableCell();
                string[] timeduration = dtRoom.Rows[i]["time"].ToString().Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);

                string[] hs = timeduration[0].Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);

                DateTime dtS = StartTime.AddDays(j).AddHours(int.Parse(hs[0])).AddMinutes(int.Parse(hs[1]));
                hs = timeduration[1].Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);

                DateTime dtE = StartTime.AddDays(j).AddHours(int.Parse(hs[0])).AddMinutes(int.Parse(hs[1]));

                string users = string.Empty;
                DataRow[] drs = dtApply.Select("AttendTimeStart='" + dtS + "' and AttendTimeEnd ='" + dtE + "'");
                foreach (DataRow item in drs)
                {
                    users += ";" + item["UserDisplayName"].ToString();
                }

                users = users.Trim(new char[] { ';' });
                TC.Text = users;
                TR.Cells.Add(TC);
            }
            TR.BorderWidth = 1;
            this.tbRoomApply.Rows.Add(TR);
        }
    }

    /// <summary>
    /// 把数字转换成星期
    /// </summary>
    /// <param name="Num">数字</param>
    /// <returns>星期</returns>
    private String ConvertNumToWeek(int Num)
    {
        switch (Num)
        {
            case 1: return "星期一";
            case 2: return "星期二";
            case 3: return "星期三";
            case 4: return "星期四";
            case 5: return "星期五";
            case 6: return "星期六";
            case 7: return "星期日";
            default: return null;
        }
    }

    /// <summary>
    /// 把星期转换成数字
    /// </summary>
    /// <param name="Week">星期</param>
    /// <returns>数字</returns>
    private int ConvertWeekToNum(String Week)
    {
        switch (Week)
        {
            case "Monday": return 0;
            case "Tuesday": return 1;
            case "Wendesday": return 2;
            case "Thursday": return 3;
            case "Friday": return 4;
            case "Saturday": return 5;
            case "Sunday": return 6;
            default: return -1;
        }
    }

    protected void btnLastWeek_Click(object sender, System.EventArgs e)
    {
        DateTime Date = DateTime.Parse(this.txtDate.Text);
        Date = Date.AddDays(-7);

        initTable(Date);
    }

    protected void btnNextWeek_Click(object sender, System.EventArgs e)
    {
        DateTime Date = DateTime.Parse(this.txtDate.Text);
        Date = Date.AddDays(7);

        initTable(Date);
    }

    protected void ddlYear_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        DateTime Date = new DateTime(int.Parse(this.ddlYear.SelectedItem.Text), int.Parse(this.ddlMonth.SelectedItem.Text), 1);
        while (!Date.DayOfWeek.ToString().Equals("Monday"))
            Date = Date.AddDays(-1);

        initTable(Date);
    }

    protected void btnCurWeek_Click(object sender, System.EventArgs e)
    {
        DateTime dtTemp = DateTime.Today;
        while (!dtTemp.DayOfWeek.ToString().Equals("Monday"))
            dtTemp = dtTemp.AddDays(-1);
        initTable(dtTemp);
    }
}
